package ua.com.jpy.services.basic;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import ua.com.jpy.entity.IEntity;

/**
 * @author LSD25
 *
 */
public abstract class AJpyBasicService implements IJpyBasicService {
	
	@Qualifier(value = "validator")
	@Autowired
	private Validator validator;
	
	@SuppressWarnings("unchecked")
	@Override
	public JSONObject validate(IEntity entityToValid) {
		Set<ConstraintViolation<IEntity>> constraintViolations = validator.validate(entityToValid);
		JSONObject json = new JSONObject();
		for(ConstraintViolation<IEntity> constraintViolation : constraintViolations) {
			json.put(constraintViolation.getPropertyPath(), constraintViolation.getMessage());
		}
		return json;
	}
	
}
